package org.example.service.imp;

import org.example.dao.imp.TeacherDaoImp;
import org.example.dao.imp.UserDaoImp;
import org.example.entitys.systems.Teacher;
import org.example.entitys.systems.User;
import org.example.service.inter.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/13
 */

@Service
public class TeacherServiceImp implements TeacherService {
    @Autowired
    public TeacherDaoImp teacherDaoImp;
    @Autowired
    public UserDaoImp userDaoImp;

    @Override
    public boolean insertTeacher(Teacher teacher) {
        if (teacherDaoImp.isExitById(teacher.getId())) return false;
        if (teacher.getLogin_id() == -1 || userDaoImp.isExitById(teacher.getLogin_id())) {
            //如果不存在相应用户
            userDaoImp.insert(new User(teacher));
            teacher.setLogin_id(userDaoImp.queryLastInsertID());
        }
        return teacherDaoImp.insert(teacher) == 1;
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        if (!teacherDaoImp.isExitById(teacher.getId())) return false;
        return teacherDaoImp.update(teacher) == 1;
    }

    @Override
    public boolean deleteTeacher(int id) {
        return teacherDaoImp.delete(id) <= 1;
    }

    @Override
    public Teacher[] queryTeacherByName(String name) {
        return teacherDaoImp.queryTeacherByName(name).toArray(new Teacher[0]);
    }

    @Override
    public Teacher queryTeacherById(int id) {
        if (!teacherDaoImp.isExitById(id)) return null;
        return teacherDaoImp.queryById(id);
    }
}
